// pages/user_proxy/team.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		data: [],
		page: 1,
		more: true,
    tab1Index: 0,
    user: {},

		tabKey: 'loadData',

		isShowQrcode: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.tryAllowShareMessage();
		this.onPullDownRefresh();
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		wx.$getServerUserInfo(true).then((res) => {
			this.setData({
				user: res
			})
		});
		this[this.data.tabKey](1).then(function() {
			wx.stopPullDownRefresh();
		});
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		if (!this.data.more) {
			return;
		}

		this[this.data.tabKey](this.data.page + 1);
  },
  onShareAppMessage () {
    return {
      title: '河南省微针植发研究院',
      path: `/pages/form/index?store_id=${this.data.user.store_id}&sale_id=${this.data.user.store.sale_id}`
    }
  },

	share: function() {
		this.setData({
			share_show: !this.data.share_show,
		})
	},

	/**
	 * 切换Tab
	 */
	tabChange: function(e) {
		const key = e.currentTarget.dataset.key;
		this.data.tabKey = key;
		this.onPullDownRefresh();
		this.setData({
			data: [],
			tabKey: key
		});
	},

	/**
	 * 切换Tab - 我的团队
	 */
	tab1Change: function(e) {
		const index = e.currentTarget.dataset.index;
		this.setData({
			tab1Index: index
		});
		this.loadData(1);
	},

	/**
	 * 加载数据
	 */
	loadData: function(page) {
		this.data.page = page;
		return wx.$model.store.myOrderList({
			status: this.data.tab1Index,
			page: page,
		}).then((res) => {
			const {data, per_page} = res;
			const oldData = this.data.data;
			this.setData({
				data: page === 1 ? data : oldData.concat(data),
				more: data.length <= per_page
			});
		}).resolved();
	},

	/**
	 * 加载我的商户
	 */
	loadChildStore: function(page) {
		this.data.page = page;
		return wx.$model.user.myChildBalanceLogs({
			page: page,
		}).then((res) => {
			const {data, per_page} = res;
			const oldData = this.data.data;
			this.setData({
				data: page === 1 ? data : oldData.concat(data),
				more: data.length <= per_page
			});
		}).resolved();
	},

	/**
	 * 显示分享二维码
	 */
	showQrcode: function() {
		if (this.data.user.store.qrcode) {
			this.setData({
				isShowQrcode: true
			})
			return;
		}

		wx.$http.get('store/getQrcode', {}, {
			loading: true
		}).then((res) => {
			this.setData({
				'user.store.qrcode': res.data,
				isShowQrcode: true
			})
		});
  },
  setValue () {
    this.setData({
      isShowQrcode: false
    })
  },
  downloadQrcode () {
    wx.getImageInfo({
      src: this.data.user.store.qrcode,
      success (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.path,
          success: function () {
            wx.showToast({
              title: '保存成功',
            })
          },
          fail (err) {
            console.log(err);
          }
        })
      }
    })
  }
})
